import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-17
 * Time: 21:37
 */
//https://leetcode.cn/problems/find-all-anagrams-in-a-string/
public class Test {
    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> list=new ArrayList();
        int[] arr=new int[26];
        int ss=s.length();
        int pp=p.length();
        int n=0;
        for(int i=0;i<pp;i++){
            if(arr[p.charAt(i)-'a']==0){
                n++;
            }
            arr[p.charAt(i)-'a']++;
        }
        int p1=0,p2=0;
        for( p2=0;p2<ss;p2++){
            int x=s.charAt(p2)-'a';
            arr[x]--;
            if(arr[x]==0){
                n--;
            }
            while(arr[x]<0){
                arr[s.charAt(p1)-'a']++;
                if(arr[s.charAt(p1)-'a']==1){
                    n++;
                }
                p1++;
            }
            if(n==0){
                list.add(p1);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        String str="cbaebabacd";
        System.out.println(findAnagrams("aa","bb").toString());
    }
}
